package org.bitmaps.cutBitmap.view
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;

	/**
	 * 截屏面板上的接触点，用来拖拉截屏面板大小
	 * @author anjue
	 */
	public class CutTouchSpot extends Sprite
	{
		public static const SPOT_MOVE:String = "spot_move";
		public static const SPOT_DOWN:String = "spot_down";
		public static const SPOT_UP:String = "spot_up";
		
		public var _index:uint = 0;
		public var spotList:Vector.<CutTouchSpot>;
		
		public var index_x:uint = 0;
		public var index_y:uint = 0;
		public var original_x:Number = 0;
		public var original_y:Number = 0;
		
		private var arrow:Sprite;
		public function CutTouchSpot()
		{
			initDisplay();
			this.addEventListener(Event.ADDED_TO_STAGE,addToStage);
		}
		
		private function addToStage(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,addToStage);
			
			this.addEventListener(MouseEvent.MOUSE_DOWN,spotDown);
			this.addEventListener(MouseEvent.MOUSE_OVER,spotOver);
			this.addEventListener(MouseEvent.MOUSE_OUT,spotOut);
		}
		
		private function initDisplay():void
		{
			var s:Sprite = new Sprite();
			s.graphics.beginFill(0);
			s.graphics.drawRect(-3,-3,6,6);
			s.graphics.endFill();
			this.addChild(s);
			this.buttonMode = true;
			
			arrow = new Sprite();
			arrow.graphics.lineStyle(2,0xFF0000);
			arrow.graphics.moveTo(0,-10);
			arrow.graphics.lineTo(0,10);
			this.addChild(arrow);
			arrow.visible = false;
		}
		
		public function set index(v:uint):void
		{
			_index = v;
			setArrowRotation();
		}
		
		public function get index():uint
		{
			return _index;
		}
		
		private var downPoint:Point;
		private var leaveStagePoint:Point;
		private function spotDown(e:MouseEvent):void
		{
			stage.addEventListener(MouseEvent.MOUSE_UP,spotUp);
			stage.addEventListener(Event.ENTER_FRAME,enterFrame);
			
			var rec:Rectangle = getSpotLimitRect();
			this.startDrag(false,rec);
			arrow.visible = true;
			Mouse.hide();
			dispatchEvent(new Event(SPOT_DOWN));
			downPoint = new Point(stage.mouseX,stage.mouseY);
		}
		
		private function enterFrame(e:Event):void
		{
			if(stage.mouseX<=0 || stage.mouseX>=stage.stageWidth || stage.mouseY<=0 || stage.mouseY>=stage.stageHeight)
			{
				leaveStagePoint = new Point(this.x,this.y);
			}else if(downPoint.x != stage.mouseX || downPoint.y != stage.mouseY)
			{
				downPoint = new Point(stage.mouseX,stage.mouseY);
				leaveStagePoint=null;
			}
			dispatchEvent(new Event(SPOT_MOVE));
			arrow.visible = true;
			Mouse.hide();
		}
		
		private function spotOver(e:MouseEvent):void
		{
			arrow.visible = true;
			Mouse.hide();
		}
		
		private function spotOut(e:MouseEvent):void
		{
			arrow.visible = false;
			Mouse.show();
		}
		
		private function spotUp(e:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP,spotUp);
			stage.removeEventListener(Event.ENTER_FRAME,enterFrame);
			this.stopDrag();
			if(leaveStagePoint)
			{
				this.x = leaveStagePoint.x;
				this.y = leaveStagePoint.y;
			}
			dispatchEvent(new Event(SPOT_UP));
			arrow.visible = false;
			Mouse.show();
		}
		
		private function getSpotLimitRect():Rectangle
		{
			var rec:Rectangle = new Rectangle(-1000,-1000,2000,2000);
			if(index == 1)
			{
				rec = new Rectangle(this.x,spotList[6].y-1005,0,1000);
			}else if(index == 6)
			{
				rec = new Rectangle(this.x,spotList[1].y+5,0,1000);
			}else if(index == 3)
			{
				rec = new Rectangle(spotList[4].x-1005,this.y,1000,0);
			}else if(index == 4)
			{
				rec = new Rectangle(spotList[3].x+5,this.y,1000,0);
			}else if(index == 0)
			{
				rec = new Rectangle(spotList[7].x-1005,spotList[7].y-1005,1000,1000);
			}else if(index == 2)
			{
				rec = new Rectangle(spotList[5].x+5,spotList[5].y-1005,1000,1000);
			}else if(index == 5)
			{
				rec = new Rectangle(spotList[2].x-1005,spotList[2].y+5,1000,1000);
			}else if(index == 7)
			{
				rec = new Rectangle(spotList[0].x+5,spotList[0].y+5,1000,1000);
			}
			return rec;
		}
		
		/**
		 * 设置箭头方向
		 */
		private function setArrowRotation():void
		{
			if(index == 1 || index == 6)
			{
				arrow.rotation = 0;
			}else if(index == 3 || index == 4)
			{
				arrow.rotation = 90;
			}else if(index == 0 || index == 7)
			{
				arrow.rotation = -45;
			}else if(index == 2 || index == 5)
			{
				arrow.rotation = 45;
			}
		}
		
		public function destroy():void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,addToStage);
			this.removeEventListener(MouseEvent.MOUSE_DOWN,spotDown);
			this.removeEventListener(MouseEvent.MOUSE_OVER,spotOver);
			this.removeEventListener(MouseEvent.MOUSE_OUT,spotOut);
			if(stage)
			{
				stage.removeEventListener(MouseEvent.MOUSE_UP,spotUp);
				stage.removeEventListener(Event.ENTER_FRAME,enterFrame);
			}
		}
	}
}